package com.fcy.teamwork.business.newproject

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fcy.teamwork.base.BaseActivity
import com.fcy.teamwork.base.toast
import com.fcy.teamwork.databinding.ActivityNewProjectBinding
import kotlinx.coroutines.launch

class NewProjectActivity : BaseActivity<ActivityNewProjectBinding>() {
    private val vm: NewProjectActVM by viewModels()
    private val adapter by lazy { ProjectInfoAdapter() }
    override fun initBinding(): ActivityNewProjectBinding =
        ActivityNewProjectBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.actionBar.tvTitle.text = "创建项目"
        binding.actionBar.ibFinish.apply {
            visibility = View.VISIBLE
            setOnClickListener {
                lifecycleScope.launch {
                    val res = vm.submitInfo(adapter.getEditedText())
                    if (res) {
                        onSubmitOk()
                    } else {
                        onSubmitError()
                    }
                }
            }
        }
        binding.rvProInfo.also {
            it.adapter = adapter
            it.layoutManager = LinearLayoutManager(this)
            adapter.submitData(vm.loadUIElement())
        }
    }

    private fun onSubmitError() {
        toast("表单未填写完整,项目创建出错")
    }

    private fun onSubmitOk() {
        toast(
            "创建项目成功"
        )
        finish()
    }


}